﻿using eFatura.Utility;
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace eFatura
{
    [Serializable]
    public class Ayar
    {
        public string GelenFaturaMaster = "";
        public string GelenFaturaDetail = "";
        public string GidenFatura = "";

        public static Ayar Load(string xmlSettings)
        {
            if (xmlSettings.Trim() == string.Empty)
                return new Ayar();

            byte[] buffer = System.Text.Encoding.GetEncoding("ISO-8859-9").GetBytes(xmlSettings);
            MemoryStream fs = new MemoryStream(buffer);
            XmlReader reader = XmlReader.Create(fs);
            XmlSerializer serializer = new XmlSerializer(typeof(Ayar));
            Ayar oAyar = (Ayar)serializer.Deserialize(reader);
            fs.Close();

            return oAyar;
        }

        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Ayar));

            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, this);
            ms.Seek(0, SeekOrigin.Begin);
            byte[] bytes = new byte[ms.Length];
            ms.Read(bytes, 0, (int)ms.Length);
            string xml = System.Text.Encoding.GetEncoding("ISO-8859-9").GetString(bytes);

            AppSession.DataManager.ExecuteNonQuery("UpdKullaniciAyar", AppSession.UserInfo.UserId, xml);
        }
    }
}